package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.CompleteInfoActivity;
import com.tj.jinzao.activity.LoginActivity;
import com.tj.jinzao.activity.MainActivity;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.callback.IRegisterLoginView;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.RegisterLoginPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.VerifyCodeInput;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 验证码录入
 * Created by wjy on 2019/1/5.
 */
public class VerifyInputFragment extends BaseFragment<RegisterLoginPresenter> implements VerifyCodeInput.Listener, IRegisterLoginView<User> {
    public final static String ARG_INPUT_TYPE = "input_type";
    public final static String ARG_PHONE = "phone";
    @BindView(R.id.code_input)
    VerifyCodeInput mCodeInputView;
    @BindView(R.id.tv_phone)
    TextView mPhoneView;

    private String mPhone, mCode;
    private int mType;//1登录，2忘记密码

    public static VerifyInputFragment newInstance(String phone, int type) {
        VerifyInputFragment fragment = new VerifyInputFragment();
        Bundle args = new Bundle(2);
        args.putInt(ARG_INPUT_TYPE, type);
        args.putString(ARG_PHONE, phone);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        super.setupViews(inflater, view);
        mPhone = getArguments().getString(ARG_PHONE);
        mType = getArguments().getInt(ARG_INPUT_TYPE);
        mPhoneView.setText("+86 " + mPhone);
        mCodeInputView.setOnCompleteListener(this);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        if (mType == 1) {
            if (mContext instanceof LoginActivity) {
                ((LoginActivity) mContext).switchFragment(LoginActivity.LOGIN_TYPE_VERITY_LOGIN);
            }
        } else {
            if (mContext instanceof LoginActivity) {
                ((LoginActivity) mContext).switchFragment(LoginActivity.LOGIN_TYPE_PASSWORD_LOGIN);
            }
        }
    }

    public void reset(String phone) {
        mPhone = phone;
        mPhoneView.setText(mPhone);
        mCodeInputView.clear();
    }

    @Override
    protected RegisterLoginPresenter createPresenter() {
        return new RegisterLoginPresenter(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_verify_input;
    }

    @OnClick(R.id.bt_confirm)
    public void confirm() {
        if (!TextUtils.isEmpty(mCode) && mCode.length() == 4) {
            if (mType == 1) {
                mPresenter.loginBySmsCode(mPhone, mCode);
            } else {
                mPresenter.validateSmsCode(mPhone, mCode);
            }
        } else {
            ToastUtils.showToast(mContext, "验证码格式不对");
        }
    }

    @Override
    public void onRequestSuccess(User user) {
        if (TextUtils.isEmpty(user.getPassword())) {
            if (mContext instanceof LoginActivity) {
                ((LoginActivity) mContext).switchFragment(LoginActivity.LOGIN_TYPE_SET_PASSWORD);
            }
            //如果性别未知，则完善资料
        } else if (3 == user.getGender()) {
            startActivity(new Intent(mContext, CompleteInfoActivity.class));
            mContext.finish();
        } else {
            startActivity(new Intent(mContext, MainActivity.class));
            mContext.finish();
        }

    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {
        ToastUtils.showToast(mContext, error_message);
    }

    @Override
    public void onGetCodeSuccess(String s) {
        //发送验证码成功，此处不用
    }

    @Override
    public void onGetCodeFailed(String reason) {
        //发送验证码失败，此处不用
    }

    @Override
    public void onCheckCodeSuccess(String token) {
        if (mContext instanceof LoginActivity) {
            ((LoginActivity) mContext).setToken(token);
            ((LoginActivity) mContext).setPhone(mPhone);
            ((LoginActivity) mContext).setSmsCode(mCode);
            ((LoginActivity) mContext).switchFragment(LoginActivity.LOGIN_TYPE_INPUT_RESET_CODE);
        }
    }

    @Override
    public void onChangeCode(String content) {
        mCode = content;
    }
}
